//
//  ViewController.m
//  self.view为nil引起的思考？
//
//  Created by Xie Wei on 2019/8/5.
//  Copyright © 2019 Xie Wei. All rights reserved.
//

#import "AController.h"
#import "BController.h"

@interface AController ()

@property (nonatomic, strong) UIView *subView;
@property (nonatomic, weak) UIView *weakView;

@end

@implementation AController

- (void)loadView {
    [super loadView];
    
    NSLog(@"%s", __FUNCTION__);
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    NSLog(@"%s", __FUNCTION__);
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    
    NSLog(@"%s", __FUNCTION__);
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    NSLog(@"%s", __FUNCTION__);
    self.title = @"self.view = nil引起的思考";
    
    self.subView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 50, 50)];
    self.subView.backgroundColor = [UIColor brownColor];
    self.subView.center = self.view.center;
    [self.view addSubview:self.subView];
    
    UIView *weakView = [[UIView alloc] initWithFrame:CGRectMake(0, 300, 50, 50)];
    weakView.backgroundColor = [UIColor yellowColor];
    [self.view addSubview:weakView];
    
    self.weakView = weakView;
    
    NSLog(@"+++++++++++++++++++ viewDidLoad 中的打印 开始++++++++++++++++++++++++");
    NSLog(@"View: %@ -- %ld", self.view, CFGetRetainCount((__bridge    CFTypeRef)(self.view)));
    NSLog(@"subView: %@ -- %ld", self.subView, CFGetRetainCount((__bridge    CFTypeRef)(self.subView)));
    NSLog(@"weakView: %@", self.weakView);
    NSLog(@"+++++++++++++++++++ viewDidLoad 中的打印 结束++++++++++++++++++++++++\r\n");
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    
    NSLog(@"%s", __FUNCTION__);
}

- (void)viewDidDisappear:(BOOL)animated {
    [super viewDidDisappear:animated];
    
    NSLog(@"%s", __FUNCTION__);
    
    self.view = nil;
    
    NSLog(@"+++++++++++++++++++ viewDidDisappear中设置 self.view = nil之后的打印 开始 ++++++++++++++++++++++++");
    NSLog(@"View: %@ -- %ld", self.view, CFGetRetainCount((__bridge    CFTypeRef)(self.view)));
    NSLog(@"subView: %@ -- %ld", self.subView, CFGetRetainCount((__bridge    CFTypeRef)(self.subView)));
    NSLog(@"weakView: %@", self.weakView);
    NSLog(@"+++++++++++++++++++ viewDidDisappear中设置 self.view = nil之后的打印 结束++++++++++++++++++++++++\r\n");
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
    NSLog(@"%s", __FUNCTION__);
    if ([[UIDevice currentDevice].systemVersion floatValue] >= 6.0) {
        if (self.isViewLoaded && !self.view.window) {
            self.view = nil;
        }
    }
}

- (IBAction)btnClick:(UIButton *)sender {
    NSLog(@"+++++++++++++++++btnClick:方法中打印 开始++++++++++++++++++++++++++");
    NSLog(@"View: %@ -- %ld", self.view, CFGetRetainCount((__bridge    CFTypeRef)(self.view)));
    NSLog(@"subView: %@ -- %ld", self.subView, CFGetRetainCount((__bridge    CFTypeRef)(self.subView)));
    NSLog(@"weakView: %@", self.weakView);
    NSLog(@"+++++++++++++++++btnClick:方法中打印 结束++++++++++++++++++++++++++\r\n");
    
    BController *vc = [[BController alloc] init];
    [self.navigationController pushViewController:vc animated:YES];
}

@end
